import java.util.HashSet;

/**
* @description: 202. 快乐数
* @author hewei
* @date 2022/7/28 16:12
* @version 1.0
*/

public class IsHappy {

    public boolean isHappy(int n) {
        HashSet<Integer> set = new HashSet<>();
        while (n != 1) {
            int temp = 0;
            while (n != 0) {
                temp += (int) Math.pow(n / 10, 2);
                n %= 10;
            }
            n = temp;
            if (!set.add(temp)) break;
        }
        return n == 1;
    }
}
